package org.example.projecttwo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.example.projecttwo.entity.FriendLink;
import org.example.projecttwo.mapper.FriendLinkMapper;
import org.example.projecttwo.service.IFriendLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 友情链接表 服务实现类
 * </p>
 *
 * @author nn
 * @since 2025-11-01
 */
@Service
public class FriendLinkServiceImpl extends ServiceImpl<FriendLinkMapper, FriendLink> implements IFriendLinkService {

    @Override
    public void batchUpdateStatus(List<Long> ids, Integer isShow) {
        FriendLink friendLink = new FriendLink();
        friendLink.setIsShow(isShow);
        this.update(friendLink, new LambdaUpdateWrapper<FriendLink>()
                .in(FriendLink::getId, ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSortOrder(List<Long> ids) {
        // 更新排序序号
        for (int i = 0; i < ids.size(); i++) {
            FriendLink friendLink = new FriendLink();
            friendLink.setId(ids.get(i));
            friendLink.setSortOrder(i + 1); // 从1开始排序
            this.updateById(friendLink);
        }
    }
}